package com.all.play.myapplication.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.all.play.myapplication.Bean.RvDataBean;
import com.all.play.myapplication.R;
import com.all.play.myapplication.adapter.MyRvAdapter;
import com.all.play.myapplication.base.BaseFragment;
import com.all.play.myapplication.view.MyGlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2018/4/18.
 * Time: 2018/4/18  15:02
 *
 *
 *
 */

public class HomeFragment extends BaseFragment {
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.rv)
    RecyclerView rv;
    Unbinder unbinder;

    //上部广告数据
    private List<Integer> images=new ArrayList<>();

    //下部rv数据
    private List<RvDataBean> rvlist=new ArrayList<>();


    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.fragment_home, null);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void initData() {
        super.initData();
        Log.i("getNetData", "initData: " + "获得主页数据");
        //上部广告数据
        getBannerData();


        //下部九宫格数据
        getRvData();

    }

    /**
     * 获取recycleview数据,并显示
     */
    private void getRvData() {

        //将数据添加紧集合
        AddDataToList();


        MyRvAdapter myRvAdapter = new MyRvAdapter(mContext,rvlist);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(layoutManager);
        rv.setAdapter(myRvAdapter);


    }

    /**
     * 将数据添加紧集合
     */
    private void AddDataToList() {
        rvlist.add(new RvDataBean(R.mipmap.s1,"商品管理"));
        rvlist.add(new RvDataBean(R.mipmap.s2,"数据库"));
        rvlist.add(new RvDataBean(R.mipmap.s3,"占位符二"));
        rvlist.add(new RvDataBean(R.mipmap.s4,"占位符三"));
        rvlist.add(new RvDataBean(R.mipmap.s5,"占位符四"));
        rvlist.add(new RvDataBean(R.mipmap.s6,"占位符五"));
        rvlist.add(new RvDataBean(R.mipmap.s7,"占位符六"));
        rvlist.add(new RvDataBean(R.mipmap.s8,"占位符七"));
        rvlist.add(new RvDataBean(R.mipmap.s9,"占位符八"));

    }

    /**
     * 回去轮播图数据
     */
    private void getBannerData() {
        images.add(R.mipmap.a);
        images.add(R.mipmap.b);
        images.add(R.mipmap.c);
        images.add(R.mipmap.d);
        images.add(R.mipmap.e);
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new MyGlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.CubeIn);
        //设置标题集合（当banner样式有显示title时）
        //banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(1500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.LEFT);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }


    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        banner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        //结束轮播
        banner.stopAutoPlay();
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
